import {Component, OnInit} from '@angular/core';
import {HttpService} from "@core/services/http.service";
import {NzMessageService} from "ng-zorro-antd";
import {ActivatedRoute, Router} from "@angular/router";
import {DomSanitizer} from "@angular/platform-browser";
import {forEach} from "@angular/router/src/utils/collection";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";

@Component({
    selector: 'app-details',
    templateUrl: './details.component.html',
    styles  : [ `
    :host ::ng-deep .vertical-center-modal {
      display: flex;
      align-items: center;
      justify-content: center;
    }

    :host ::ng-deep .vertical-center-modal .ant-modal {
      top: 0; 
    }
  ` ]
})

export class DetailsComponent implements OnInit {
    loading = false;
    searchValue = '';
    data = [];
    param: any;
    id;
    pid;
    type;
    modalVisible2 = false;
    url = this.http.BASE_URL;
    Score = '';
    Comment = '';
    form: FormGroup;

    ngOnInit() {
        this.form = this.fb.group({
            score: [null, [Validators.required]],
            comment: [null, [Validators.required]]
        },);
        this.getData();
    }

    getData() {
        this.ActiveRoute.params.subscribe(res => {
            this.id = res.id;
            this.pid = res.pid
            this.type = res.type;
        });

        this.param = {
            // Id: this.id,
            // pId: this.pid
            Id: 17,
            pId: 2
        }

        this.loading = true;
        this.http.exercisesStudentInfo(this.param).subscribe(res => {
            if (res['code'] == '0') {
                // for (let i = 0; i < res['stulist'].length; i++) {
                //     res['stulist'][i].imagepath = this.sanitizer.bypassSecurityTrustResourceUrl(this.http.BASE_URL + res['stulist'][i].imagepath);
                // }
                this.data = res['stulist'];
            } else {
                this.msgService.error(res['msg']);
            }
        });
        this.loading = false;
    }

    getFormControl(name) {
        return this.form.controls[name];
    }

    save() {
        // for (const i in this.form.controls) {
        //     this.form.controls[i].markAsDirty();
        // }
        // if (this.form.valid) {
        this.loading = true;
        // if (this.saveState === 'add') {

        this.param = {
            id: '28',
            score: this.Score,
            comment: this.Comment
        }
        this.http.addStudentComment(this.param).subscribe(res => {
            if (res['code'] == '0') {
                this.msgService.success(res['msg']);
                // this.router.navigate(['home']);
                this.getData();
                this.modalVisible2 = false;
                this.Score='',
                this.Comment=''
            } else {
                this.msgService.error(res['msg']);
            }
        });

        // }else {
        //     this.param = {
        //         id: this.S_id.toString() ,
        //         UserName: this.form.value.glyName + '-管理员',
        //         LoginName: this.form.value.glyName,
        //         LoginPwd: this.form.value.pass,
        //         ContactWay: this.form.value.tel,
        //         Type: '2',
        //         SchoolName: this.form.value.xxName,
        //         CollageName: this.form.value.xyName
        //     }
        //     this.http.userUpdate(this.param).subscribe(res => {
        //         if (res['code'] == '0') {
        //             this.msgService.success(res['msg']);
        //             this.getData();
        //             this.modalVisible2 = false;
        //         } else {
        //             this.msgService.error(res['msg']);
        //         }
        //     });
        // }
        this.loading = false;
        // }
    }

    constructor(private http: HttpService,
                private msgService: NzMessageService,
                private fb: FormBuilder,
                private ActiveRoute: ActivatedRoute) {
    }
}
